package com.hw.weathershow.controller;

import com.hw.weathershow.entity.Weather;
import com.hw.weathershow.factory.ProduceFactory;
import com.hw.weathershow.factory.ServiceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * The type Query weather controller.
 *
 * @author Cejay1314
 * @version 1.0
 * @create 2022 /12/4 12:05
 */
//@Api(tags = "天气查询接口")
@RestController
@RequestMapping("/")
public class QueryWeatherController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ProduceFactory produceFactory;
    @Autowired
    private ServiceFactory serviceFactory;

    /**
     * Query weather string.
     *
     * @return the string
     */
//    @ApiOperation(value = "天气查询接口", notes = "返回最近7天天气预报")
    @GetMapping
    public String QueryWeather() {
        String apiURL = "https://restapi.amap.com/v3/weather/weatherInfo?city=510100&key=3303c29ca8378c7c93dcf768bbc50f95";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiURL, String.class);

        if (200 == responseEntity.getStatusCodeValue()) {
            return responseEntity.getBody();
        } else {
            return "error with code : " + responseEntity.getStatusCodeValue();
        }
    }

    /**
     * Query weather weather.
     *
     * @return the weather
     */
    @GetMapping("/queryweather")
    public Weather queryWeather(){
        return serviceFactory.getQueryWeatherService().queryWeather();
    }

    /**
     * Broadcast.
     */
    @GetMapping("/broadcast")
    public void broadcast(){
        produceFactory.getFactory("UtilFactory").getObserver("WeatherObserver").update();
    }

    /**
     * Hello string.
     *
     * @return the string
     */
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
}